﻿using UnityEngine;
using System.Collections;

using System.Collections.Generic;
using Login;
#if !NAKED
using supersdkUnity;
public class PlatformSDK {
    
    private static PlatformSDK _instance = null;
    private const string urlEnd = "main/checkPlatformToken";//拼接用的
    #region 初始化用到参数
    private const string appid = "com.yunchang.djsy";//appid(app唯一标示)
    private const string appSecret = "lw=`kFlB~zC+!mjz8hg6";//SuperSDK验证参数//old,37玩用过：@wx#cjx$ds%^&*IKLsa1
    private const string privateKey = "C&7?$Pg6.x]$T4*_;u#J";//SuperSDK验证参数//old，37玩用过：$CwK*&$#FVdKsdc
    private const string gameObjectUserCallBack = "PlatformSDKCallBack";//用户消息回掉的对象名称
    private const string userCallBackFun = "UserCallBackFun";//用户消息会掉的函数名称
    private const string gameObjectPayCallBack = "PlatformSDKCallBack";//支付消息回掉的对象名称
    private const string payCallBackFun = "PayCallBackFun";//支付消息会掉的函数名称
    private bool isDebug = true;//是否输出日志信息，tag为“SuperSDK”
    #endregion

    

    /// <summary>
    /// 单例
    /// </summary>
    /// <returns></returns>
    public static PlatformSDK GetInstance() 
    {
        if (_instance == null) 
        {
            _instance = new PlatformSDK();
        }
        return _instance;
    }

   public LoginManager manager;

    /// <summary>
    /// 初始化
    /// </summary>
    public void Init() 
    {
        Debug.Log("init begin");
        
        SuperSDKInitInfo data = new SuperSDKInitInfo();
        data.setAppId(appid);
        data.setAppSecret(appSecret);
        data.setPrivateKey(privateKey);
        data.setGameObjectUserCallBack(gameObjectUserCallBack);
        data.setGameObjectPayCallBack(gameObjectPayCallBack);
        data.setUserCallBackFun(userCallBackFun);
        data.setPayCallBackFun(payCallBackFun);
        data.setSuperDebug(isDebug);
        SuperSDKUnity3D.init(data);
        Debug.Log("end");
    }
    /// <summary>
    /// 登录（qq登录）
    /// </summary>
    /// <param name="manager"></param>
    public void OtherLogin(LoginManager manager) 
    {
        Debug.Log("[DEBUG] PlatformSDK OtherLogin");
        this.manager = manager;
        Dictionary<string, string> loginData = new Dictionary<string, string>();
        //loginData.Add(SuperSDKDataKeys.LoginInfo.SERVER_ID, "");
        loginData.Add(SuperSDKDataKeys.LoginInfo.LOGIN_REQURL, ServerConfig.GetServerUrl() + urlEnd);
        loginData.Add(SuperSDKDataKeys.LoginInfo.DATA, "appData");
        SuperSDKUnity3D.call(SuperSDKDataKeys.Function.OTHER_LOGIN, loginData);

    }

    /// <summary>
    /// 登录(普通登录和微信登录)
    /// </summary>
    public void Login(LoginManager manager) 
    {
        Debug.Log("[DEBUG] PlatformSDK Login");
        this.manager = manager;
        Dictionary<string, string> loginData = new Dictionary<string, string>();
        //loginData.Add(SuperSDKDataKeys.LoginInfo.SERVER_ID,StaticData.playerData.ZoneId.ToString() );//服务器ID
        loginData.Add(SuperSDKDataKeys.LoginInfo.LOGIN_REQURL, ServerConfig.GetServerUrl() + urlEnd);//login requrl
        //Debug.Log("PlatformSDK Url = " + ServerConfig.GetServerUrl() + urlEnd);
        loginData.Add(SuperSDKDataKeys.LoginInfo.DATA, "appData");//appData
        SuperSDKUnity3D.login(loginData);
    }

    /// <summary>
    /// 注销
    /// </summary>
    public void Logout() 
    {
        SuperSDKUnity3D.call(SuperSDKDataKeys.Function.LOGOUT);
    }

    /// <summary>
    /// 显示悬浮窗
    /// </summary>
    public void ShowFloatButton() 
    {
        Dictionary<string, string> pos = new Dictionary<string, string>();
        pos.Add(SuperSDKDataKeys.FloatButton.POSITION, SuperSDKDataKeys.ButtonPosition.TOP_LEFT);
        SuperSDKUnity3D.call(SuperSDKDataKeys.Function.SHOW_FLOAT_BUTTON, pos);
    }

    /// <summary>
    /// 隐藏悬浮窗
    /// </summary>
    public void HideFloatButton() 
    {
    SuperSDKUnity3D.call(SuperSDKDataKeys.Function.HIDE_FLOAT_BUTTON);
    }

    /// <summary>
    /// 进入平台中心
    /// </summary>
    public void EnterPlatform() 
    {
        SuperSDKUnity3D.call(SuperSDKDataKeys.Function.ENTER_PLATFORM);
    }

    /// <summary>
    /// 切换账号
    /// </summary>
    public void SwitchAccount() 
    {
        GameConfig.Instance.isSwitch = true;
        //ManagerController.Instance.BackToLogin();
        SuperSDKUnity3D.call(SuperSDKDataKeys.Function.SWITCH_ACCOUNT);
    }

    /// <summary>
    /// 退出游戏
    /// </summary>
    public void Exit() 
    {
        SuperSDKUnity3D.call(SuperSDKDataKeys.Function.EXIT);
    }

    /// <summary>
    /// 登录时候提交用户数据
    /// </summary>
    public void SubmitData() 
    {
        Dictionary<string, string> submitData = new Dictionary<string, string>();
        submitData.Add(SuperSDKDataKeys.SubmitInfo.ROLE_ID, StaticData.playerData.playerId.ToString());//提交角色ID
        submitData.Add(SuperSDKDataKeys.SubmitInfo.ROLE_NAME, StaticData.playerData.name);//提交角色名
        submitData.Add(SuperSDKDataKeys.SubmitInfo.ROLE_LEVEL, StaticData.playerData.level.ToString());//提交角色等级
        submitData.Add(SuperSDKDataKeys.SubmitInfo.ROLE_BALANCE, StaticData.playerData.diamond.ToString());//角色余额
        submitData.Add(SuperSDKDataKeys.SubmitInfo.ROLE_VIP, StaticData.playerData.vip.ToString());//角色VIP等级
        submitData.Add(SuperSDKDataKeys.SubmitInfo.ZONE_ID, StaticData.playerData.ZoneId.ToString());//服务器ID（必须大于1）
        submitData.Add(SuperSDKDataKeys.SubmitInfo.ZONE_NAME, manager.ZoneName);//服务器名称
        submitData.Add("roleCTime",StaticData.playerData.createRoleTime);
        submitData.Add("role_create_time", StaticData.playerData.createRoleTime);
        if (GameConfig.Instance.GetChannelId == ChannelId.HuoCheWIFI)
        {
            submitData.Add("role_level_up_time", StaticData.playerData.createRoleTime);
        }
        SuperSDKUnity3D.call(SuperSDKDataKeys.Function.SUBMIT_DATA, submitData);
       
    }
    /// <summary>
    /// 角色升级提交数据
    /// </summary>
    public void LevelUpSubmit()
    {
        Dictionary<string, string> submitData = new Dictionary<string, string>();
        submitData.Add(SuperSDKDataKeys.SubmitInfo.ROLE_ID, StaticData.playerData.playerId.ToString());//提交角色ID
        submitData.Add(SuperSDKDataKeys.SubmitInfo.ROLE_NAME, StaticData.playerData.name);//提交角色名
        submitData.Add(SuperSDKDataKeys.SubmitInfo.ROLE_LEVEL, StaticData.playerData.level.ToString());//提交角色等级
        submitData.Add(SuperSDKDataKeys.SubmitInfo.ROLE_BALANCE, StaticData.playerData.diamond.ToString());//角色余额
        submitData.Add(SuperSDKDataKeys.SubmitInfo.ROLE_VIP, StaticData.playerData.vip.ToString());//角色VIP等级
        submitData.Add(SuperSDKDataKeys.SubmitInfo.ZONE_ID, StaticData.playerData.ZoneId.ToString());//服务器ID（必须大于1）
        submitData.Add(SuperSDKDataKeys.SubmitInfo.ZONE_NAME, manager.ZoneName);//服务器名称
        submitData.Add("roleCTime", StaticData.playerData.createRoleTime);
        submitData.Add("role_create_time", StaticData.playerData.createRoleTime);
        submitData.Add(SuperSDKDataKeys.SubmitInfo.EXT, "LevelUp");//如果在角色升级时，请传入此值，其他时候，请勿传递
        SuperSDKUnity3D.call(SuperSDKDataKeys.Function.SUBMIT_DATA, submitData);
    }
    /// <summary>
    /// 创建用户提交数据
    /// </summary>
    public void CreateRoleSubmitData()
    {
        Dictionary<string, string> submitData = new Dictionary<string, string>();
        submitData.Add(SuperSDKDataKeys.SubmitInfo.ROLE_ID, StaticData.playerData.playerId.ToString());//提交角色ID
        submitData.Add(SuperSDKDataKeys.SubmitInfo.ROLE_NAME, StaticData.playerData.name);//提交角色名
        submitData.Add(SuperSDKDataKeys.SubmitInfo.ROLE_LEVEL, StaticData.playerData.level.ToString());//提交角色等级
        submitData.Add(SuperSDKDataKeys.SubmitInfo.ROLE_BALANCE, StaticData.playerData.diamond.ToString());//角色余额
        submitData.Add(SuperSDKDataKeys.SubmitInfo.ROLE_VIP, StaticData.playerData.vip.ToString());//角色VIP等级
        submitData.Add(SuperSDKDataKeys.SubmitInfo.ZONE_ID, StaticData.playerData.ZoneId.ToString());//服务器ID（必须大于1）
        submitData.Add(SuperSDKDataKeys.SubmitInfo.ZONE_NAME, manager.ZoneName);//服务器名称
        submitData.Add("roleCTime", StaticData.playerData.createRoleTime);
        submitData.Add("role_create_time", StaticData.playerData.createRoleTime);
        submitData.Add(SuperSDKDataKeys.SubmitInfo.EXT, "CreateRole");
        SuperSDKUnity3D.call(SuperSDKDataKeys.Function.SUBMIT_DATA, submitData);

    }
	/// <summary>
	/// ios登录成功
	/// </summary>
	public void IOSLoginSubmitData(){
		Dictionary<string, string> submitData = new Dictionary<string, string>();
		submitData.Add(SuperSDKDataKeys.SubmitInfo.ROLE_ID, StaticData.playerData.playerId.ToString());//提交角色ID
		submitData.Add(SuperSDKDataKeys.SubmitInfo.ROLE_NAME, StaticData.playerData.name);//提交角色名
		submitData.Add(SuperSDKDataKeys.SubmitInfo.ROLE_LEVEL, StaticData.playerData.level.ToString());//提交角色等级
		submitData.Add(SuperSDKDataKeys.SubmitInfo.ROLE_BALANCE, StaticData.playerData.diamond.ToString());//角色余额
		submitData.Add(SuperSDKDataKeys.SubmitInfo.ROLE_VIP, StaticData.playerData.vip.ToString());//角色VIP等级
		submitData.Add(SuperSDKDataKeys.SubmitInfo.ZONE_ID, StaticData.playerData.ZoneId.ToString());//服务器ID（必须大于1）
		submitData.Add(SuperSDKDataKeys.SubmitInfo.ZONE_NAME, manager.ZoneName);//服务器名称
		Dictionary<string,string> loginExtra = new Dictionary<string, string>();
		loginExtra.Add ("eventType", "loginEvent");
		submitData.Add (SuperSDKDataKeys.SubmitInfo.EXT, JsonMapper.ToJson (loginExtra));
		SuperSDKUnity3D.call(SuperSDKDataKeys.Function.SUBMIT_DATA, submitData);

	}
    /// <summary>
    /// ios支付成功
    /// </summary>
    /// <param name="orderId"></param>
    /// <param name="orderAmount"></param>
	public void IOSPaySubmitData(string orderId,string orderAmount){
	
		Dictionary<string, string> submitData = new Dictionary<string, string>();
		submitData.Add(SuperSDKDataKeys.SubmitInfo.ROLE_ID, StaticData.playerData.playerId.ToString());//提交角色ID
		submitData.Add(SuperSDKDataKeys.SubmitInfo.ROLE_NAME, StaticData.playerData.name);//提交角色名
		submitData.Add(SuperSDKDataKeys.SubmitInfo.ROLE_LEVEL, StaticData.playerData.level.ToString());//提交角色等级
		submitData.Add(SuperSDKDataKeys.SubmitInfo.ROLE_BALANCE, StaticData.playerData.diamond.ToString());//角色余额
		submitData.Add(SuperSDKDataKeys.SubmitInfo.ROLE_VIP, StaticData.playerData.vip.ToString());//角色VIP等级
		submitData.Add(SuperSDKDataKeys.SubmitInfo.ZONE_ID, StaticData.playerData.ZoneId.ToString());//服务器ID（必须大于1）
		submitData.Add(SuperSDKDataKeys.SubmitInfo.ZONE_NAME, manager.ZoneName);//服务器名称
		Dictionary<string,string> payExtra = new Dictionary<string, string>();
		payExtra.Add ("eventType", "payEvent");
		payExtra.Add ("orderId", orderId);
		payExtra.Add ("orderAmount", orderAmount);
		submitData.Add (SuperSDKDataKeys.SubmitInfo.EXT, JsonMapper.ToJson (payExtra));
		SuperSDKUnity3D.call (SuperSDKDataKeys.Function.SUBMIT_DATA, submitData);

	}
    /// <summary>
    /// 退出游戏提交数据
    /// </summary>
    public void ExitGameSubmit() 
    {
        Dictionary<string, string> submitData = new Dictionary<string, string>();
        submitData.Add(SuperSDKDataKeys.SubmitInfo.ROLE_ID, StaticData.playerData.playerId.ToString());//提交角色ID
        submitData.Add(SuperSDKDataKeys.SubmitInfo.ROLE_NAME, StaticData.playerData.name);//提交角色名
        submitData.Add(SuperSDKDataKeys.SubmitInfo.ROLE_LEVEL, StaticData.playerData.level.ToString());//提交角色等级
        submitData.Add(SuperSDKDataKeys.SubmitInfo.ROLE_BALANCE, StaticData.playerData.diamond.ToString());//角色余额
        submitData.Add(SuperSDKDataKeys.SubmitInfo.ROLE_VIP, StaticData.playerData.vip.ToString());//角色VIP等级
        submitData.Add(SuperSDKDataKeys.SubmitInfo.ZONE_ID, StaticData.playerData.ZoneId.ToString());//服务器ID（必须大于1）
        submitData.Add(SuperSDKDataKeys.SubmitInfo.ZONE_NAME, manager.ZoneName);//服务器名称
        submitData.Add("roleCTime", StaticData.playerData.createRoleTime);
        submitData.Add("role_create_time", StaticData.playerData.createRoleTime);
        submitData.Add(SuperSDKDataKeys.SubmitInfo.EXT, "Exit");//如果在退出游戏时，请传入此值，其他时候，请勿传递
        SuperSDKUnity3D.call(SuperSDKDataKeys.Function.SUBMIT_DATA, submitData);
    }
    /// <summary>
    /// 实名注册，360特有
    /// </summary>
    public void RealNameRegister() 
    {
        SuperSDKUnity3D.call(SuperSDKDataKeys.Function.REALNAME_REGISTER);
    }

    /// <summary>
    /// 防沉迷，360特有
    /// </summary>
    public void AntiaddictionQuery() 
    {
        SuperSDKUnity3D.call(SuperSDKDataKeys.Function.ANTIADDICTION_QUERY);
    }

    /// <summary>
    /// 重新启动程序
    /// </summary>
    public void RestartApp() 
    {
        SuperSDKUnity3D.restartApp();
        //SuperSDKPlugin.Restart("");
    }

    #region 获取Manifest信息

    /// <summary>
    /// 获取VersionCode
    /// </summary>
    public string GetVersionCode() 
    {
       return SuperSDKUnity3D.getVersionCode();
    }

    /// <summary>
    /// 获取VersionName
    /// </summary>
    /// <returns></returns>
    public string GetVersionName() 
    {
        return SuperSDKUnity3D.getVersionName();
    }

    /// <summary>
    /// 获取Manifest自定义字段
    /// </summary>
    /// <returns></returns>
    public string UserDefinedValue() 
    {
        return SuperSDKUnity3D.getUserDefinedValue("custom_key");
    }

    /// <summary>
    /// 获取SuperSDK定义的channel ID
    /// </summary>
    /// <returns></returns>
    public string SuperSDKChannelId() 
    {
        return SuperSDKUnity3D.getUserDefinedValue("SSDK_CHANNEL_ID");
    }

    #endregion

    public void GenerateOrder(Dictionary<string, string> payInfoDict)
    {
        string productId = "";
#if UNITY_IOS
        BoxManager.CreateNetMask();
#endif
        Debug.Log("generateOrder begin");
        string appData = payInfoDict["appData"];
        string superUid = payInfoDict["superUid"];
        string serverId = payInfoDict["serverId"];
        string roleName = payInfoDict["roleName"];
        string roleId = payInfoDict["roleId"];
        string roleLevel = payInfoDict["roleLevel"];
        string roleCoin = payInfoDict["roleCoin"];
        string productName = payInfoDict["productName"];


        if (GameConfig.Instance.GetChannelId == ChannelId.LianXiang)
        {
            productId = "75924";
        }
        else if (GameConfig.Instance.GetChannelId == ChannelId.YYH)
        {
            productId = "10";
        }
        else
        {
            productId = payInfoDict["productId"];
        }
        //productId = payInfoDict["productId"];
        string productPrice = payInfoDict["productPrice"];
        string productNum = payInfoDict["productNum"];
        string gamePlayerUid = payInfoDict["gamePlayerUid"];
        SuperSDKPayment payInfo = new SuperSDKPayment();
        payInfo.setAppData(appData);//透传参数，由客户端传递，最终传给APP服务器
        payInfo.setUid(superUid);//SuperSDK用户对应的唯一身份表标识，从用户登录毁掉接口获取
        payInfo.setServerId(int.Parse(serverId));//服务器ID，必须为大于0的整数；
        //角色名称
        payInfo.setRoleName(roleName);
        //角色ID，必须为整形字符串
        payInfo.setRoleId(roleId);
        //角色等级,整数
        payInfo.setRoleGrade(int.Parse(roleLevel));
        //角色钻石，元宝，金币数等
        payInfo.setRoleBalance(roleCoin);
        //商品名称
        payInfo.setProductName(productName);
        //商品ID
        payInfo.setProductId(productId);
        //商品价格
        payInfo.setOrderAmount(productPrice);
        //商品数量
        payInfo.setProductCount(int.Parse(productNum));
        //app定义的玩家角色对应的唯一ID
        payInfo.setGameUid(gamePlayerUid);

        Debug.Log("chargeData productId = " + productId);
        Debug.Log("chargeData productPrice = " + productPrice);
        Debug.Log("chargeData productName = " + productName);
        Debug.Log("chargeData roleId = " + roleId);


        SuperSDKUnity3D.pay(payInfo);
        Debug.Log("generateOrder begin2");


    }

}
#endif